<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/mock-min.js"></script>
    <script src="../js/mock.js"></script>
    <style>
        .bigbox{
            font-size: 20px;
            width: 350px;
            background-color: aqua;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="bigbox">
        茶名称
        <input type="text"><br>
        茶价格
        <input type="number" value="0"><br>
        茶类型
        <select name="" id="lei">
            <option value="0">绿茶</option>
            <option value="1">黄茶</option>
            <option value="2">乌龙茶</option>
            <option value="3">红茶</option>
            <option value="4">黑茶</option>
            <option value="5">白茶</option>
            <option value="6">花茶</option>
        </select>
        <br>
        <input type="file" name="" id="forfile" onchange="downloadfile()" >
        <br>
        <button onclick="addroom()">添加</button>
    </div>
    <script>
        var arr=[]
        class Rooms{
            constructor(id,name,price,lei,img) {
                this.id=id
                this.name=name
                this.price=price
                this.lei=lei
                this.img=img
            }
        }
        const downloadfile=()=>{
        var file=    document.getElementById('forfile').files
        console.log(file[0].name);
        }
        function pirceiftrue(){
            var price=parseInt(document.getElementsByTagName('input')[1].value)
            return price>=0 ? true : false
        }
        
        function endtosto(){
            let room=new Rooms()
            var leilist=document.getElementById('lei').options
            var inpone=document.getElementsByTagName('input')[0].value
            var inptwo=document.getElementsByTagName('input')[1].value
            room.name=inpone
            room.price=inptwo
            for (const key in leilist) {
                    if(leilist[key].selected==true){
                        room.lei=leilist[key].innerText
                    }
                }
               return room
        }
        function addroom(){
            if(pirceiftrue()){
                let room=new Rooms()
                room=endtosto()
                room.img='https://ccdn1.goodq.top/caches/9b69daffc17579b848483c854813cad2/aHR0cHM6Ly81Y2RkM2FhNWM0NmVhLnQ3My5xaWZlaXllLmNvbS9xZnktY29udGVudC91cGxvYWRzLzIwMTkvMDQvODMxNGZmNmYyYjkyNzcxZmY2N2U5NjdlZjQyNjAzZWItMTAyNHgxMDI0LTkwLndlYnA_p_p100_p_3D.webp'
                $.ajax({
                type:'post',
                url:'/js/roomadd',
                data:JSON.stringify(room),
                success:function(data){
                    location.href='./roomshow.html'
                }
            })
            }else{
                alert('价格必须>=0')
            }
           
        }
    </script>
</body>
</html>